﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
//using System.ServiceModel;
using System.Text;
using System.Xml;
using System.Web;

namespace M139.Api
{
    [Serializable]
    public class M139ApiService : oAuthM139//,IM139ApiService
    {
        //public string Token { get; set; }
        //public string TokenSecret { get; set; }
        //private oAuthM139 oauth = new oAuthM139();

        public M139ApiService()
        {
            //default format
            Format = "xml";
        }

        public void CheckToken()
        {

        }

        //从新浪跳转回来，换取Access Token
        public bool oAuthWeb(string oauth_token, string oauth_verifier)
        {
            Verifier = oauth_verifier;
            AccessTokenGet(oauth_token);
            return true;
        }


        /// <summary>
        /// 这个是供桌面端应用调用的方法，需要用户提供用户名和密码
        /// </summary>
        /// <returns></returns>
        public bool oAuthDesktop(string userid, string passwd)
        {
            try
            {
                string authLink = AuthorizationGet();
                authLink += "&userId=" + userid + "&passwd=" + passwd + "&action=submit&oauth_callback=none";
                string html = WebRequest(Method.POST, authLink, null);
                string pin = ParseHtml(html);
                Verifier = pin;
                AccessTokenGet(Token);
                return true;
            }
            catch
            {
                return false;
            }
        }

        /**********************************************************************************************
         *************************************微博访问接口*********************************************
         **********************************************************************************************
         **********************************************************************************************/
        /*获取单条ID的微博信息*/
        public Status status_getinfo(int id)
        {
            try
            {
                string url = "http://talkapi.shequ.10086.cn/timeline/getinfo." + Format;
                string response = oAuthWebRequest(Method.GET, url, "id=" + id);

                var xmlDoc = new XmlDocument();
                xmlDoc.LoadXml(response);
                Status status = new Status();
                status.id = Convert.ToInt32(xmlDoc.GetElementsByTagName("id")[0].InnerText);
                status.text = xmlDoc.GetElementsByTagName("text")[0].InnerText;
                status.type = xmlDoc.GetElementsByTagName("type")[0].InnerText;
                status.source = xmlDoc.GetElementsByTagName("source")[0].InnerText;
                status.repost_count = int.Parse(xmlDoc.GetElementsByTagName("repost_count")[0].InnerText);
                status.comment_count = int.Parse(xmlDoc.GetElementsByTagName("comment_count")[0].InnerText);
                status.user_id = int.Parse(xmlDoc.GetElementsByTagName("user_id")[0].InnerText);
                status.create_at = xmlDoc.GetElementsByTagName("create_at")[0].InnerText;

                return status;
            }
            catch
            { return null; }
        }
        /*发布一条微博信息*/
        public Status posttext(string text)
        {
            try
            {
                string url = "http://talkapi.shequ.10086.cn/timeline/posttext." + Format + "?";
                string response = oAuthWebRequest(Method.POST, url, "text=" + text);

                var xmlDoc = new XmlDocument();
                xmlDoc.LoadXml(response);
                Status status = new Status();
                status.id = Convert.ToInt32(xmlDoc.GetElementsByTagName("id")[0].InnerText);
                status.text = xmlDoc.GetElementsByTagName("text")[0].InnerText;
                status.type = xmlDoc.GetElementsByTagName("type")[0].InnerText;
                status.source = xmlDoc.GetElementsByTagName("source")[0].InnerText;
                status.repost_count = int.Parse(xmlDoc.GetElementsByTagName("repost_count")[0].InnerText);
                status.comment_count = int.Parse(xmlDoc.GetElementsByTagName("comment_count")[0].InnerText);
                status.user_id = int.Parse(xmlDoc.GetElementsByTagName("user_id")[0].InnerText);
                status.create_at = xmlDoc.GetElementsByTagName("create_at")[0].InnerText;

                return status;
            }
            catch
            { return null; }
        }
        /*上传图片并发布一条微博信息 */
        public Status postpict(string text, string pic)
        {
            try
            {
                string url = "http://talkapi.shequ.10086.cn/timeline/postpict." + Format + "?";
                string response = oAuthWebRequest(Method.POST, url, "text=" + text + "&pic=" + HttpUtility.UrlEncode(pic));

                var xmlDoc = new XmlDocument();
                xmlDoc.LoadXml(response);
                Status status = new Status();
                status.id = Convert.ToInt32(xmlDoc.GetElementsByTagName("id")[0].InnerText);
                status.text = xmlDoc.GetElementsByTagName("text")[0].InnerText;
                status.type = xmlDoc.GetElementsByTagName("type")[0].InnerText;
                status.source = xmlDoc.GetElementsByTagName("source")[0].InnerText;
                status.repost_count = int.Parse(xmlDoc.GetElementsByTagName("repost_count")[0].InnerText);
                status.comment_count = int.Parse(xmlDoc.GetElementsByTagName("comment_count")[0].InnerText);
                status.user_id = int.Parse(xmlDoc.GetElementsByTagName("user_id")[0].InnerText);
                status.create_at = xmlDoc.GetElementsByTagName("create_at")[0].InnerText;
                status.photo_url = xmlDoc.GetElementsByTagName("photo_url")[0].ChildNodes[0].InnerText;

                return status;
            }
            catch
            { return null; }
        }
        /*获取用户个人资料*/
        public User user_getinfo()
        {
            try
            {
                string url = "http://talkapi.shequ.10086.cn/user/getinfo." + Format;
                string response = oAuthWebRequest(Method.GET, url, String.Empty);

                var xmlDoc = new XmlDocument();
                xmlDoc.LoadXml(response);
                User user = new User();
                user.id = Convert.ToInt32(xmlDoc.GetElementsByTagName("id")[0].InnerText);
                user.screen_name = xmlDoc.GetElementsByTagName("screen_name")[0].InnerText;
                user.nickname = xmlDoc.GetElementsByTagName("nickname")[0].InnerText;
                user.province = xmlDoc.GetElementsByTagName("province")[0].InnerText;
                user.city = xmlDoc.GetElementsByTagName("city")[0].InnerText;
                user.description = xmlDoc.GetElementsByTagName("description")[0].InnerText;
                user.icon24 = xmlDoc.GetElementsByTagName("icon24")[0].InnerText;
                user.icon80 = xmlDoc.GetElementsByTagName("icon80")[0].InnerText;
                user.gender = int.Parse(xmlDoc.GetElementsByTagName("gender")[0].InnerText);
                user.medal_wear = xmlDoc.GetElementsByTagName("medal_wear")[0].InnerText;
                user.medal_count = Convert.ToInt32(xmlDoc.GetElementsByTagName("medal_count")[0].InnerText);

                user.verified_title = xmlDoc.GetElementsByTagName("verified_title")[0].InnerText;
                user.verified_type = Convert.ToInt32(xmlDoc.GetElementsByTagName("verified_type")[0].InnerText);
                user.created_at = xmlDoc.GetElementsByTagName("created_at")[0].InnerText;
                return user;
            }
            catch
            { return null; }
        }
    }
}